#include "qimagewidget.h"
#include <QMouseEvent>

QImageWidget::QImageWidget(QWidget* parent) : QWidget(parent)
{
    this->image = new QImage();
    this->setSize(0,0);
}

QImageWidget::QImageWidget(QImage* image, QWidget* parent) : QWidget(parent)
{
    this->setImage(image);
}

QImageWidget::QImageWidget(QImage image, QWidget* parent) : QWidget(parent)
{
    this->setImage(image);
}

QImageWidget::QImageWidget(QString path, QWidget* parent) : QWidget(parent)
{
    this->setImage(path);
}

QImageWidget::~QImageWidget()
{
    delete this->image;
}

void QImageWidget::setImage(QImage* image)
{
    if (image)
    {
        delete this->image;
        this->image = new QImage(*image);
        this->setSize(image->size());
        this->update();
    }
}

void QImageWidget::setImage(QImage image)
{
    this->setImage(&image);
}

void QImageWidget::setImage(QString path)
{
    this->setImage(new QImage(path));
}

void QImageWidget::setSize(QSize size)
{
    this->setMinimumSize(size);
    this->setMaximumSize(size);
}

void QImageWidget::setSize(int height, int width)
{
    this->setSize(QSize(width, height));
}

QSize QImageWidget::getSize() const
{
    return this->image->size();
}

int QImageWidget::getHeiht()
{
    return this->image->height();
}

int QImageWidget::getWidth()
{
    return this->image->width();
}

QImage QImageWidget::getImage(QRect* region) const
{
    if (region)
    {
        return this->image->copy(*region);
    }
    return QImage(*(this->image));
}

void QImageWidget::paintEvent(QPaintEvent* )
{
    QPainter painter(this);
    painter.drawImage(QPoint(0,0),*(this->image));
}

void QImageWidget::mouseReleaseEvent(QMouseEvent *event)
{
    QPoint point = event->pos();
    emit pointClicked(point);
}


